setwd("/home1/30/jc227089/SRE/gvar/ran_outs")

ncomb<-90
#first concatenation
# to generate, for each parameter combo, mean across reps of mean (across time) of trait values through space
concat<-c()
for (i in 1:90){
	load(paste("gvarout", i, "RData", sep=""))
	temp<-do.call("rbind", sim.result[3,])
	repl<-rep(1:length(sim.result[1,]), times= unlist(lapply(sim.result[3,], length))/length(sim.result[3,][[1]][1,]))
	temp<-cbind(repl, temp)
	summ<-tapply(temp[,"popsize"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.ps<-apply(summ, 1, mean)
	summ.ps.sd<-apply(summ, 1, sd)
	summ<-tapply(temp[,"mean.disp"], list(temp[,"xloc"], temp[,"repl"]), mean, na.rm=T)
	summ.tv<-apply(summ, 1, mean)
	summ.tv.sd<-apply(summ, 1, sd)
	pars<-sim.result[2,1][[1]][1,1:4]
	out<-t(matrix(pars, ncol=length(summ.tv), nrow=length(pars)))
	out<-cbind(out, 1:length(summ.ps), summ.ps, summ.ps.sd, summ.tv, summ.tv.sd)
	colnames(out)<-c(names(pars), "xloc", "ps.mean", "ps.sd", "tv.mean", "tv.sd")
	concat<-rbind(concat, out)
	rm(sim.result)	
}

save(concat, file="acSpace.RData")


